package com.wuyong.chapter9;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 查询不可变数组任意区间的和
 */
public class LeetCode303 {

    private static class NewArr {
        private Integer[] sums;
        public NewArr(Integer[] arr) {
            sums = new Integer[arr.length + 1];
            sums[0] = 0;
            for (int i = 1; i < arr.length; i++) {
                sums[i] = sums[i - 1] + arr[i - 1];
            }
        }

        public int sumRange(int i, int j){
            return sums[j + 1] - sums[i];
        }

    }

    public static void main(String[] args) {
        Integer[] arr = {1, 3, 5, 6, -4, 3};
        NewArr newArr = new NewArr(arr);
        System.out.println("sums = [" + newArr.sumRange(0, 4) + "]");
    }

}
